// 背包显示类


var UIBag = DraggablePanelBase.extend({

	_bagPos : null,

	_data : null,
	_items : {},

    ctor : function (bagPos) {
    	this._super(0);

    	this._bagPos = bagPos;
	},

	// 显示数据, 刷新一次
	setData : function (data) {
        var i = 0;
        for each(var d in data) {
            var cx = parseInt(i % helper.BAG_WIDTH);
            var cy = parseInt(i / helper.BAG_WIDTH);

            var item = new UIBagItem(d)
            item.setBagPos(cx, cy);
            this.addChild(item);

            this._items[d.id] = item;
            i += 1;
        }
	},

    // 移动之后调用, 用来同步数据
    onItemMove : function (item) {
        var t = item;
        for each(var d in this._data) {
            if(d.id == t.id) {
                d.cx = t.cx;
                d.cy = t.cy;
            }
        }

        //* 发送网络数据? 通知DataManager

    },

    getItemByCellPos : function (cx, cy) {
        //cc.log('getTankByCellPos:'+cx+'  '+cy);
        for(var id in this._items) {
            var t = this._items[id];
            var p = t.getBagPos();
            if (p.x == cx && p.y == cy) return t;
        }
    },

    onTouchBegan : function (touch, event) {
        //cc.log('GarageTouchPanel.onTouchBegan');
       
        var tpos = touch.getLocation();

        if(this._isTouchIn(tpos)) {
            var cp = this._touchPos2Cell(tpos);

            // 如果点击的位置有坦克
            var t = this.getItemByCellPos(cp.x, cp.y);
            if (t) {
                this.dragBegin(t);  // 开始拖曳
                return true;
            }
        }
        return false;
    },

    onTouchMoved : function (touch, event) {
        this._super(touch, event);
    },

    onTouchEnded : function (touch, event) {
        //cc.log('GarageTouchPanel.onTouchEnded');
        if (!this.isDragging()) return;

        var tpos = touch.getLocation();
        //cc.dump(tpos);
        if(this._isTouchIn(tpos)) {
            var cp = this._touchPos2Cell(tpos);
            // 如果点击的位置有坦克
            var t = this.getItemByCellPos(cp.x, cp.y);
            if (t) {
                // 还原
                this.dragEnd(true);
            } else {
                var dragging = this.getDragTarget();
                this.dragEnd(); // 结束拖曳
                dragging.setBagPos(cp.x, cp.y);    // 放入新位置
                
                // 坦克移动
                this.onItemMove(dragging);
            }
        } 
        else {
            // 界外
            this.dragEnd(true);
        }
    },

    // 点击在范围内
    _isTouchIn : function (touchPos) {
        var p = cc.pSub(touchPos, this._bagPos);
     	var w = helper.BAG_CELL_SIZE * helper.BAG_WIDTH;
     	var h = helper.BAG_CELL_SIZE * helper.BAG_HEIGHT;
        return (p.x >= 0 && p.x < w && p.y >= 0 && p.y < h);
    },

    // touch坐标转成Cell坐标
    _touchPos2Cell : function (touchPos) {
        var p = cc.pSub(touchPos, this._bagPos);
        var cx = parseInt(p.x / helper.BAG_CELL_SIZE);
        var cy = helper.BAG_HEIGHT - 1 - parseInt(p.y / helper.BAG_CELL_SIZE);
        return cc.p(cx, cy);
    }, 

});
